﻿using System;

namespace NDI
{
    internal class ServiceKey
    {
        public ServiceKey(Type serviceType, Type factoryType)
        {
            this.ServiceType = serviceType;
            this.FactoryType = factoryType;
        }

        public Type ServiceType { get; private set; }
        public Type FactoryType { get; private set; }
        public string Name { get; set; }

        #region Equality

        public static bool Equals(ServiceKey obj1, ServiceKey obj2)
        {
            if (Object.Equals(null, obj1) || Object.Equals(null, obj2) || obj1.GetType() != obj2.GetType())
            {
                return false;
            }

            if (Object.ReferenceEquals(obj1, obj2))
            {
                return true;
            }

            //Compare your object properties
            //Code Examples:
            return obj1.ServiceType == obj2.ServiceType && obj1.FactoryType == obj2.FactoryType && obj1.Name == obj2.Name;
        }

        public bool Equals(ServiceKey other)
        {
            return ServiceKey.Equals(this, other);
        }

        public override bool Equals(Object obj)
        {
            return ServiceKey.Equals(this, obj as ServiceKey);
        }

        public override int GetHashCode()
        {
            //Implement your hashing typically by combining the hashes of your fields
            //Code Examples:
            int hash = ServiceType.GetHashCode();
            hash ^= FactoryType.GetHashCode();
            if (Name != null)
            {
                hash ^= Name.GetHashCode();
            }
            return hash;
        }

        public static bool operator ==(ServiceKey obj1, ServiceKey obj2)
        {
            if (Object.ReferenceEquals(obj1, obj2))
            {
                return true;
            }

            if (obj1 == null || obj2 == null)
            {
                return false;
            }

            return obj1.ServiceType == obj2.ServiceType && obj1.FactoryType == obj2.FactoryType && obj1.Name == obj2.Name;
        }

        public static bool operator !=(ServiceKey obj1, ServiceKey obj2)
        {
            return !(obj1 == obj2);
        }
        #endregion
    }
}
